//package com.chart.web.controller.chart;
//
//import com.alibaba.fastjson2.JSON;
//import com.chart.chart.service.impl.NativePayService;
//import com.chart.common.config.WechatpayConfig;
//import com.chart.common.core.controller.BaseController;
//import com.chart.common.utils.QRCodeUtils;
//import com.chart.common.utils.v3payCore.util.RSAAutoCertificateConfig;
//import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
//import com.wechat.pay.java.core.Config;
//
//import com.wechat.pay.java.service.payments.nativepay.model.Amount;
//import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
//import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletResponse;
//import java.nio.charset.StandardCharsets;
//import java.security.GeneralSecurityException;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * 四重奏图
// *
// * @author chart
// */
//@RestController
//@RequestMapping("/wechart")
//public class WechatPayV3Controller extends BaseController
//{
//
//    @Autowired
//    private WechatpayConfig wechatpayConfig;
//
//
//    public static NativePayService service;
//
//    /**
//     * Native下单
//     *  调用统一下单API，生成支付二维码
//     */
//    @PostMapping("/native")
//    public void nativePay(HttpServletResponse response) throws Exception {
//
//        // 初始化商户配置
//        Config config =
//                new RSAAutoCertificateConfig.Builder()
//                        .merchantId(wechatpayConfig.getMchId())
//                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
//                        .privateKeyFromPath(wechatpayConfig.getPrivateKeyPath())
//                        .merchantSerialNumber(wechatpayConfig.getMchSerialNo())
//                        .apiV3Key(wechatpayConfig.getApiV3Key())
//                        .build();
//
//        service = new NativePayService.Builder().config(config).build();
//
//        PrepayResponse prepay = prepay();
//        ServletOutputStream outputStream = response.getOutputStream();
//        QRCodeUtils.encode(prepay.getCodeUrl(),outputStream);
//
//    }
//
//        /**
//     * 支付通知
//     * 微信支付通过支付通知接口将用户支付成功消息通知给商户
//     */
//    @PostMapping("/native/notify")
//    public Map<String,String> nativeNotify(@RequestBody Map<String,Object> signalRes, HttpServletResponse response){
//        Map<String, String> map = new HashMap<>();//应答对象
//        try {
//            //用密文解密出明文
//            Map<String,String> resource=(Map<String,String>)signalRes.get("resource");
//            String ciphertext=resource.get("ciphertext");
//            String associatedData=resource.get("associated_data");
//            String nonce=resource.get("nonce");
//            // 拿到明文
//            String plainText=new AesUtil(wechatpayConfig.getApiV3Key().getBytes(StandardCharsets.UTF_8)).decryptToString(associatedData.getBytes(StandardCharsets.UTF_8),nonce.getBytes(StandardCharsets.UTF_8),ciphertext);
//            //转换
//            HashMap<String,Object> data= JSON.parseObject(plainText,HashMap.class);
//            //处理订单
//            //成功应答：成功应答必须为200或204，否则就是失败应答
//            response.setStatus(200);
//            map.put("code", "SUCCESS");
//        } catch (GeneralSecurityException e) {
//            response.setStatus(500);
//            map.put("code", "FAIL");
//            map.put("message","失败");
//        }
//        return map;
//    }
//
//    /** Native支付预下单 */
//    public PrepayResponse prepay() {
//        PrepayRequest request = new PrepayRequest();
//        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
//        // 调用接口
//        request = new PrepayRequest();
//        Amount amount = new Amount();
//        amount.setTotal(1);
//        request.setAmount(amount);
//        request.setAppid(wechatpayConfig.getAppid());
//        request.setMchid(wechatpayConfig.getMchId());
//        request.setDescription("测试商品标题");
//        request.setNotifyUrl(wechatpayConfig.getNotifyDomain()+"/wechart/native/notify");
//        request.setOutTradeNo("out_trade_no_0012");
//        return service.prepay(request);
//    }
//
//}
